package com.smarthome.rpc.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.smarthome.common.exception.ExceptionUtils;
import com.smarthome.domain.bean.ShSmsRecord;
import com.smarthome.domain.common.Constants;
import com.smarthome.domain.enums.SmsExceptionEnum;
import com.smarthome.rpc.SmsService;
import com.smarthome.rpc.base.BaseRpcService;
import com.smarthome.rpc.sms.SmsClient;

/**
 * 
 * <B>Description</B> 短信 服务 <br />
 * <B>Copyright</B> Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
 * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
 * <B>Company</B> xxxx公司
 */
@Service("smsService")
public class SmsServiceImpl extends BaseRpcService implements SmsService {

    @Override
    public void sendSms(ShSmsRecord record) {
    	if(null == record) {
    		LOGGER.info(SmsExceptionEnum.ERROR.showMessage("无可发送的短信"));
    		ExceptionUtils.throwSms(SmsExceptionEnum.NUMBER_NOTTRUE);
    	}
    	if(null == record.getUrid()){
    		record.setUrid(System.currentTimeMillis() +"");
    	}
    	if(null == record.getMobile() || !record.getMobile().matches(Constants.PHONE_REG)){ //手机号码初步验证
    		LOGGER.info(SmsExceptionEnum.NUMBER_NOTTRUE.showMessage(record.getMobile()));
    		record.setErrorDate( new Date());
    		record.setErrorMsg(SmsExceptionEnum.NUMBER_NOTTRUE.showMessage(record.getMobile()));
    		record.setStatus(2);
    		ExceptionUtils.throwSms(SmsExceptionEnum.NUMBER_NOTTRUE);
    	}
        // TODO:调用外部短信服务发送短信
    	//String mt = SmsClient.mt(mobilestr, content, "", "", urid);
    	String trnCode = SmsClient.sendSMS(record.getMobile(), record.getContent(), record.getUrid());
    	//String mt = urid;//用于测试
    	
    	if(SmsClient.SUCCESS_CODE.equals(trnCode)){
    		record.setStatus(4);
    	} else {
    		record.setStatus(2);
    		record.setErrorDate( new Date());
    		record.setErrorMsg(SmsExceptionEnum.ERROR.showMessage(trnCode));
    	}
    }

	/* (non-Javadoc)
	 * @see com.smarthome.rpc.SmsService#butchSendSms(java.lang.String, java.lang.String[])
	 */
	@Override
	public String butchSendSms(String content, String... mobiles) {
		String mobilestr = "";
    	String urid = "sms:" + System.currentTimeMillis();
    	
    	for(String s : mobiles){
    		if(s.matches(Constants.PHONE_REG)){ //手机号码初步验证
    			if("".equals(mobilestr)) mobilestr.concat(",");
    			mobilestr.concat(s);
    		} else {
    			LOGGER.info(SmsExceptionEnum.NUMBER_NOTTRUE.showMessage(s));
    			ExceptionUtils.throwSms(SmsExceptionEnum.NUMBER_NOTTRUE);
    		}
    	}
        // TODO:调用外部短信服务发送短信
    	//String mt = SmsClient.mt(mobilestr, content, "", "", urid);
    	String mt = urid;//用于测试
    	if(urid.equals(mt)){
    		LOGGER.info(mobilestr + ":" + content);
    	} else {
    		LOGGER.info(SmsExceptionEnum.getSmsException(mt).showMessage(mobiles));
    		ExceptionUtils.throwSms(SmsExceptionEnum.getSmsException(mt));
    	}
    	
    	return mt;
	}
}
